<?php
/**
 * 综合行政
 * Enter description here ...
 * @author cc
 *
 */
namespace Home\Controller;
use Think\Controller;
class AdministrativeController extends CommonController{
	public function way(){
		session("action","Administrative");
		session("menu_id",I('get.menu_id',0,'intval'));
		$this->redirect("Public/index");
	}
	

	public function supplies(){
		$user_name = session('user_name');
		$office_supplies_list = M("office_supplies_list");
		$office_supplies_log = M("office_supplies_log");
		$content = I('post.content');
		if(empty($content)){
			$data_list = $office_supplies_list->select();
		}else{
			$data_list = $office_supplies_list->where("supplies_name like '%s'",$content)->select();
		}
		$model = M();
		foreach ($data_list as $key=>$value){
			$a_num =0; $b_num = 0;
			$supplies_id = $value['id'];
			$sql1 = 'select sum(num) as sum from oa_office_supplies_log where supplies_id='.$supplies_id.' and type=1 group by supplies_id,num,time';
			$data1 = $model->query($sql1);
			foreach ( $data1 as $v ){
				$a_num += $v;
			}
			$sum1 = $data1['0']['sum'];
			$sql2 = 'select sum(num) as sum from oa_office_supplies_log where supplies_id='.$supplies_id.' and type=2 group by supplies_id,num,time';
			$data2 = $model->query($sql2);
			foreach ( $data2 as $v2 ){
				$b_num += $v2;
			}
			$data_list[$key]['sum'] = $a_num-$b_num;
		}
//		$sql3 = "select dept.dept_name from oa_sys_user user,oa_sys_dept dept where user.user_name='$user_name' and user.dept_id=dept.id limit 1";
//		$sql4 = "select priv.priv_name from oa_sys_user user,oa_sys_priv priv where user.user_name='$user_name' and user.priv_id=priv.id limit 1";
//		$dept_name = $model->query($sql3);
//		$priv_name = $model->query($sql4);
//		if($dept_name['0']['dept_name'] == '行政部' || $priv_name['0']['priv_name'] == "OA管理员"){
//			$quanxian = 1;
//		}else{
//			$quanxian = 2;
//		}
		
		
		$quanxian = 1;
		$this->assign("content", $content);
		$this->assign('quanxian',$quanxian);
		$this->assign('data_list',$data_list);
		$this->assign("empty",emptyhtml("list"));
		$this->display();
	}
	

	public function supplies_add(){
		$this->display();
	}
	
	public function supplies_insert(){
		if(!IS_POST){ $this->error("非法表单"); }
		$data = array(
			'supplies_name' => $_POST['supplies_name'],
			'specification' => $_POST['specification'],
			'unit' => $_POST['unit'],
			'unit_price' => $_POST['unit_price'],
			'min_number' => $_POST['min_number']
		);
		$office_supplies_list = M("office_supplies_list");
		$success = $office_supplies_list->add($data);
		if($success){
    		$this->systemlog('add', '办公物品', "添加办公物品信息---".$_POST['supplies_name']);
    		$this->success('添加办公物品信息成功！','supplies');
    	}else {
    		$this->error("添加办公物品信息失败");
    	}
	}
	
	public function supplies_edit(){
		$id = I('get.id');
		if(!is_id($id)){$this->error("非法参数！");}
		$office_supplies_list = M("office_supplies_list");
		$data_supplies = $office_supplies_list->where('id=%d',$id)->find();
		$this->assign('data',$data_supplies);
		$this->display();
	}
	
	public function supplies_save(){
		$id = I('post.id');
		if(!IS_POST){ $this->error("非法表单"); }
		if(!is_id($id)){$this->error("非法参数！");}
		
		$office_supplies_list = M("office_supplies_list");
		$data = array(
			'supplies_name' => $_POST['supplies_name'],
			'specification' => $_POST['specification'],
			'unit' => $_POST['unit'],
			'unit_price' => $_POST['unit_price'],
			'min_number' => $_POST['min_number']
		);
		$success = $office_supplies_list->where('id=%d',$id)->save($data);
		if($success){
    		$this->systemlog('edit', '办公物品', "修改办公物品信息---".$_POST['supplies_name']);
    		$this->success('修改办公物品信息成功！','supplies');
    	}else {
    		$this->error("修改办公物品信息失败");
    	}
	}
	
	public function supplies_delete(){
		$id = I('get.id');
		if(!is_id($id)){$this->error("非法参数！");}
		
		$office_supplies_list = M("office_supplies_list");
		$supplies_name = $office_supplies_list->where('id=%d',$id)->getField("supplies_name");
		$success = $office_supplies_list->where('id=%d',$id)->delete();
		if($success){
    		$this->systemlog('delete', '办公物品', "删除办公物品信息---".$supplies_name);
    		$this->success('删除办公物品信息成功！','supplies');
    	}else {
    		$this->error("删除办公物品信息失败");
    	}
	}
	
	public function supplies_in_list(){//入库明细
		$content = $_POST['content'];
		if(!is_name($content)){$this->error("查询条件只能是汉字！");}
		$model = M();
		if(empty($content)){
			$sql = "SELECT log.id log_id, log.*,list.*,user.name FROM oa_office_supplies_log log,oa_office_supplies_list list,oa_sys_user user where log.supplies_id=list.id and log.user_name=user.user_name and log.type=1 order by time desc";
		}else{
			$sql = "SELECT log.id log_id, log.*,list.*,user.name FROM oa_office_supplies_log log,oa_office_supplies_list list,oa_sys_user user where log.supplies_id=list.id and log.user_name=user.user_name and log.type=1 and list.supplies_name like '%$content%' order by time desc";
		}
		$count = count($model->query($sql));
		$page = new \Think\Page($count,C("PAGE_LIST_ROWS"));
		$show = $page->show();//分页输出显示
		$sql .= " limit $page->firstRow,$page->listRows";
		$data = $model->query($sql);
		$this->assign('data',$data);
		$this->assign('page',$show);
		$this->assign("empty",emptyhtml("list"));
		$this->display();
	} 
	
	public function supplies_out_list(){//出库明细
		$content = $_POST['content'];
		if(!is_name($content)){$this->error("查询条件只能是汉字！");}
		$model = M();
		if(empty($content)){
			$sql = "SELECT log.*,list.*,user.name FROM oa_office_supplies_log log,oa_office_supplies_list list,oa_sys_user user where log.supplies_id=list.id and log.user_name=user.user_name and log.type=2 order by time desc";
		}else{
			$sql = "SELECT log.*,list.*,user.name FROM oa_office_supplies_log log,oa_office_supplies_list list,oa_sys_user user where log.supplies_id=list.id and log.user_name=user.user_name and log.type=2 and list.supplies_name like '%$content%' order by time desc";
		}
		
		$count = count($model->query($sql));
		$page = new \Think\Page($count,C("PAGE_LIST_ROWS"));
		$show = $page->show();//分页输出显示
		$sql .= " limit $page->firstRow,$page->listRows";
		$data = $model->query($sql);
		$this->assign('data',$data);
		$this->assign('page',$show);
		$this->display();
	}
	
	public function supplies_in_check(){//物品入库选择
		$office_supplies_list = M("office_supplies_list");
		$office_supplies_log = M("office_supplies_log");
		$content = $_POST['content'];
		$data_list = $office_supplies_list->select();
		$model = M();
		foreach ($data_list as $key=>$value){
			$supplies_id = $value['id'];
			$sql1 = 'select sum(num) as sum from oa_office_supplies_log where supplies_id='.$supplies_id.' and type=1';
			$data1 = $model->query($sql1);
			$sum1 = $data1['0']['sum'];
			$sql2 = 'select sum(num) as sum from oa_office_supplies_log where supplies_id='.$supplies_id.' and type=2';
			$data2 = $model->query($sql2);
			$sum2 = $data2['0']['sum'];
			$data_list[$key]['sum'] = $sum1-$sum2;
		}
		$this->assign('data_list',$data_list);
		$this->display();
	}
	
	public function supplies_use_check(){//物品领用
		$tpl = I('get.tpl') ? I('get.tpl') : 1;
		$office_supplies_list = M("office_supplies_list");
		$office_supplies_log = M("office_supplies_log");
		$content = $_POST['content'];
		if(empty($content)){
			$data_list = $office_supplies_list->select();
		}else{
			$data_list = $office_supplies_list->where("supplies_name like '$content'")->select();
		}
		$model = M();
		foreach ($data_list as $key=>$value){
			$supplies_id = $value['id'];
			$sql1 = 'select sum(num) as sum from oa_office_supplies_log where supplies_id='.$supplies_id.' and type=1';
			$data1 = $model->query($sql1);
			$sum1 = $data1['0']['sum'];
			$sql2 = 'select sum(num) as sum from oa_office_supplies_log where supplies_id='.$supplies_id.' and type=2';
			$data2 = $model->query($sql2);
			$sum2 = $data2['0']['sum'];
			$data_list[$key]['sum'] = $sum1-$sum2;
		}
		$this->assign('tpl',$tpl);
		$this->assign('data_list',$data_list);
		$this->assign("empty",emptyhtml("list"));
		$this->display();
	}
	
	public function supplies_in_add(){
		$supplies_id = $_POST['supplies_id'];
		$sum = $_POST['sum'];
		$office_supplies_list = M("office_supplies_list");
		$model = M();
		for ($i=0;$i<count($supplies_id);$i++){  
			$data[$i] = $office_supplies_list->where('id='.$supplies_id[$i])->find();
			$sql1 = 'select sum(num) as sum from oa_office_supplies_log where supplies_id='.$supplies_id[$i].' and type=1';
			$data1 = $model->query($sql1);
			$sum1 = $data1['0']['sum'];
			$sql2 = 'select sum(num) as sum from oa_office_supplies_log where supplies_id='.$supplies_id[$i].' and type=2';
			$data2 = $model->query($sql2);
			$sum2 = $data2['0']['sum'];
			$data[$i]['sum'] = $sum1-$sum2;
		}
		$count = count($data);
		$this->assign('data',$data);
		$this->assign('count',$count);
		$this->assign('empty',emptyhtml('list'));
		$this->display();
	}
	
	public function supplies_in_insert(){
		if(!IS_POST){ $this->error("非法表单！");exit; }
		$time = date('Y-m-d');
		$supplies_id = $_POST['supplies_id'];
		$number = $_POST['number'];
		//dump($number);
		$office_supplies_list = M("office_supplies_list");
		for ($i=0;$i<count($supplies_id);$i++){
			$supplies_name = $office_supplies_list->where('id='.$supplies_id[$i])->getField('supplies_name');
			$id = $supplies_id[$i];
			if(empty($number[$i])){
				$number[$i] = '0';
			}
			$data = array(
				'supplies_id' => $id,
				'num' => $number[$i],
				'time' => $time,
				'user_name' => session('user_name'),
				'type' => '1'
			);
			$oa_office_supplies_log = M('office_supplies_log');
			$oa_office_supplies_log->add($data);
			$this->systemlog('add', '办公物品', "添加办公物品---".$supplies_name);
//			$data[] = "编号:".$id.",名称:".$supplies_name.",数量:".$number[$i];
		}
		$this->success('添加成功','supplies_in_list');
	}
	
	public function supplies_use_add(){
		$tpl = I('get.tpl') ? I('get.tpl') : 1;
		$supplies_id = $_POST['supplies_id'];
		$sum = $_POST['sum'];
		$office_supplies_list = M("office_supplies_list");
		$model = M();
		for ($i=0;$i<count($supplies_id);$i++){  
			$data[$i] = $office_supplies_list->where('id='.$supplies_id[$i])->find();
			$sql1 = 'select sum(num) as sum from oa_office_supplies_log where supplies_id='.$supplies_id[$i].' and type=1';
			$data1 = $model->query($sql1);
			$sum1 = $data1['0']['sum'];
			$sql2 = 'select sum(num) as sum from oa_office_supplies_log where supplies_id='.$supplies_id[$i].' and type=2';
			$data2 = $model->query($sql2);
			$sum2 = $data2['0']['sum'];
			$data[$i]['sum'] = $sum1-$sum2;
		}
		$count = count($data);
		$this->assign('tpl',$tpl);
		$this->assign('data',$data);
		$this->assign('count',$count);
		$this->display();
	}
	
	public function supplies_use_insert(){
		if(!IS_POST){ $this->error("非法表单！");exit; }
		$supplies_id = $_POST['supplies_id'];
		$number = $_POST['number'];
		//dump($number);
		$office_supplies_list = M("office_supplies_list");
		for ($i=0;$i<count($supplies_id);$i++){
			$supplies_name = $office_supplies_list->where('id='.$supplies_id[$i])->getField('supplies_name');
			$id = $supplies_id[$i];
			if(empty($number[$i])){
				$number[$i] = '0';
			}
			$data[] = "编号:".$id.",名称:".$supplies_name.",数量:".$number[$i];
		}
		$wpsqqd = implode(';&#13', $data);
		session('wpsqqd',$wpsqqd);
//		echo session('wpsqqd');
		echo "<script>alert('窗口关闭之后，请在填写的表单页面，鼠标右键->刷新');</script>";
		echo "<script>window.close();</script>";
	}

	public function supplies_log_delete(){
		$id = I('get.id');
		$office_supplies_log = M("office_supplies_log");
		$success = $office_supplies_log->where('id='.$id)->delete();
		if($success){
			
    		$this->systemlog('delete', '入库明细', "删除办公物品入库信息");
    		$this->success('删除办公物品入库信息成功！','supplies_in_list');
    	}else {
    		$this->error("删除办公物品入库信息失败");
    	}
	}
	
	public function travel(){
		$time = date('Y-m');
		$datetime = $_POST['datetime']?$_POST['datetime']:$time;
		$time1 = date('Ym',strtotime($datetime));
		//出差记录
		$sql = "select data.data_value from oa_workflow_work work, oa_workflow_work_prcs prcs, oa_workflow_work_data data where work.type_id=3 and work.work_id=prcs.work_id and prcs.proc_no !=1 and date_format(work.work_time,'%Y-%m')='$datetime' and data.work_id=work.work_id group by data.work_id";
		$model = M();
		$data = $model->query($sql);
		if(!empty($data)){
			foreach ($data as $key1=>$value1){
				$data_value1 = explode('***', $value1['data_value']);
				$number = sprintf("%03d",($key1+1));
				$data[$key1]['number'] = 'PLD-CC'.$time1.$number;
				$data[$key1]['name'] = $data_value1['1'];
				$data[$key1]['dept_name'] = $data_value1['2'];
				$data[$key1]['send_time'] = $data_value1['0'];
				$data[$key1]['start_time'] = $data_value1['7'];
				$data[$key1]['end_time'] = $data_value1['8'];
				$data[$key1]['count_time'] = $data_value1['3'];
				$data[$key1]['start_add'] = $data_value1['5'];
				$data[$key1]['end_add'] = $data_value1['6'];
				$data[$key1]['traffic'] = $data_value1['11'];
			}
		}
		$this->assign('data',$data);
		$this->assign('datetime',$datetime);
		$this->assign("empty",emptyhtml("list"));
		$this->display();
	}
	
	public function download(){
		Vendor('phpexcel.PHPExcel');
//		Vendor('phpexcel.PHPExcel','','.class.php');
		$objExcel = new \PHPExcel();
		
		$objExcel->getProperties()->setCreator("office 2003 excel");
		$objExcel->getProperties()->setLastModifiedBy("office 2003 excel");
		$objExcel->getProperties()->setTitle("Office 2003 XLS Test Document");
		$objExcel->getProperties()->setSubject("Office 2003 XLS Test Document");
		$objExcel->getProperties()->setDescription("Test document for Office 2003 XLS, generated using PHP classes.");
		$objExcel->getProperties()->setKeywords("office 2003 openxml php");
		$objExcel->getProperties()->setCategory("Test result file");
		$objExcel->setActiveSheetIndex(0);
		$objActSheet = $objExcel->getActiveSheet();
		
		$objActSheet->setTitle("办公物品清单");//设置当前sheet
		
		$objActSheet->setCellValue('A1','物品名称');
		$objActSheet->setCellValue('B1','规格');
		$objActSheet->setCellValue('C1','单位');
		$objActSheet->setCellValue('D1','单价');
		$objActSheet->setCellValue('E1','库存');
		
		$office_supplies_list = M("office_supplies_list");
		$data_list = $office_supplies_list->select();
		$data_count = $office_supplies_list->count();
		$model = M();
		$i = 1;
		
		foreach ($data_list as $key=>$value){
			$supplies_id = $value['id'];
			$sql1 = 'select sum(num) as sum from oa_office_supplies_log where supplies_id='.$supplies_id.' and type=1';
			$data1 = $model->query($sql1);
			$sum1 = $data1['0']['sum'];
			$sql2 = 'select sum(num) as sum from oa_office_supplies_log where supplies_id='.$supplies_id.' and type=2';
			$data2 = $model->query($sql2);
			$sum2 = $data2['0']['sum'];
			$sum = $sum1-$sum2;
			
			$i++;
			$objActSheet->setCellValue('A'.$i, $value['supplies_name']);
			$objActSheet->setCellValue('B'.$i, $value['specification']);
			$objActSheet->setCellValue('C'.$i, $value['unit']);
			$objActSheet->setCellValue('D'.$i, $value['unit_price']);
			$objActSheet->setCellValue('E'.$i, $sum);
		}

		$arrs = array('A','B','C','D','E');
		for($i=1;$i<($data_count+2);$i++){
	       for($j=0;$j<5;$j++){
	           $objActSheet->getStyle($arrs[$j].$i)->getBorders()->getAllBorders()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
	           $objActSheet->getStyle($arrs[$j].$i)->getBorders()->getAllBorders()->getColor()->setARGB('FF00BBcc');
			   $objActSheet->getStyle($arrs[$j].$i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
	       }
   		}
		
 		//设置列的宽度
		$objActSheet->getColumnDimension('A')->setWidth(30);
		$objActSheet->getColumnDimension('B')->setWidth(10);
		$objActSheet->getColumnDimension('C')->setWidth(15);
		$objActSheet->getColumnDimension('D')->setWidth(10);
		$objActSheet->getColumnDimension('E')->setWidth(10);
   		
   		// 设置页方向和规模
		$objActSheet->getPageSetup()->setOrientation(\PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT);
		$objActSheet->getPageSetup()->setPaperSize(\PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
   		
		$excelName = '办公物品清单';
		header('Content-Type: application/vnd.ms-excel');
	    header('Cache-Control: max-age=0');
	    header('Content-Disposition: attachment; filename='.iconv("utf-8", "GBK", $excelName).'.xls');
	    $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
	    $objWriter->save('php://output');
	}
    
    public function _empty($name){
    	$this->emptyaction($name);
    }
}
?>